package com.example.a2022application.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


import com.example.a2022application.BaseActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.ClassEntity;
import com.example.a2022application.bean.ClassesBean;
import com.example.a2022application.bean.CourseBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.storage.CourseStorage;
import com.example.a2022application.utils.SpUtils;
import com.example.a2022application.utils.SpUtils2;
import com.example.a2022application.widget.ClassScheduleCard;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课表
 */
public class WorkActivity extends BaseActivity {

    private int pagenum = 1;
    private Map<Integer, List<ClassEntity>> datas;

    private ClassScheduleCard classScheduleCard;

    private String classname;

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work);
        classScheduleCard = findViewById(R.id.class_schedule_card);
        spinner = findViewById(R.id.spinner);
        initData();
    }

    public void initData() {
        String spIdentity = SpUtils.getString(WorkActivity.this, "spIdentity");
        if ("管理员".equals(spIdentity)){
            spinner.setVisibility(View.VISIBLE);
            getClasses();
        }else if ("学生".equals(spIdentity)){
            spinner.setVisibility(View.GONE);
            classname = SpUtils.getString(WorkActivity.this, "classname");
            getTimetable();
        }
    }

    private void getTimetable() {
        String obj = SpUtils.getString(WorkActivity.this, "userid");
        OkGo.<String>post(Urls.TIMETABLELIST)
//                .params("userId", obj)
                .params("class_name", classname)
                .params("pageNum", pagenum)
                .params("pageSize", 100)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        CourseBean bean = new Gson().fromJson(response.body(), CourseBean.class);
                        Log.e("TAG","课表2---"+response.body());
                        if (bean.getCode() == 0) {
                            CourseStorage storage = new CourseStorage();
                            storage.clear();
                            datas = new HashMap<>();
                            for(int i=0; i<bean.getData().size(); i++){

                                String time = bean.getData().get(i).getTime();
                                String week = bean.getData().get(i).getWeek();
                                String course_name = bean.getData().get(i).getCourse_name();
                                String class_name = bean.getData().get(i).getClass_name();
                                int start_node = Integer.parseInt(bean.getData().get(i).getStart_node());
                                int end_node = Integer.parseInt(bean.getData().get(i).getEnd_node());

                                ClassEntity classEntity = new ClassEntity();
                                classEntity.setObj_id(bean.getData().get(i).getObj_id());
                                classEntity.setTime(time);
                                classEntity.setCourseName(course_name);
                                classEntity.setClassNamae(class_name);
                                classEntity.setStartNode(start_node);
                                classEntity.setEndNode(end_node);
                                classEntity.setState(1);
                                classEntity.setWeek(week);

                                int key = 0;
                                if("星期一".equals(week)){
                                    key = 1;
                                }else if("星期二".equals(week)){
                                    key = 2;
                                }else if("星期三".equals(week)){
                                    key = 3;
                                }else if("星期四".equals(week)){
                                    key = 4;
                                }else if("星期五".equals(week)){
                                    key = 5;
                                }else if("星期六".equals(week)){
                                    key = 6;
                                }else if("星期日".equals(week)){
                                    key = 7;
                                }else if("星期天".equals(week)){
                                    key = 7;
                                }
                                List<ClassEntity> list = datas.get(key);
                                if(list == null){
                                    list = new ArrayList<>();
                                }
                                list.add(classEntity);
                                datas.put(key, list);
                            }

                            for (Integer key : datas.keySet()) {;
                                List<ClassEntity> classEntities = datas.get(key);
                                Log.e("set------->",classEntities.toString());
                                storage.setValue(key, classEntities);
                            }
                            classScheduleCard.initViews();

                        } else {
                        }
                    }
                });
    }

    private void getClasses() {
        OkGo.<String>post(Urls.SELECTCLASS)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ClassesBean bean = getGson().fromJson(response.body(), ClassesBean.class);
                        if (bean.getCode() == 0){
                            List<String> strarr = new ArrayList<>();
                            for (int i = 0;i<bean.getData().size();i++){
                                strarr.add(bean.getData().get(i).getClass_name());
                            }
                            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(WorkActivity.this,
                                    android.R.layout.simple_spinner_item, strarr);
                            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spinner.setAdapter(dataAdapter);
                            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                    classname = strarr.get(i);
                                    getTimetable();
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> adapterView) {

                                }
                            });
                        }
                    }
                });
    }

    //返回
    public void back(View view){
        finish();
    }


}